package com.hk.Mgr.web.controller.kyc;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.QuestionReq;
import com.hk.Mgr.web.message.kyc.QuestionRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.entity.Question;
import com.hk.commons.utils.yingmi.YingmiApiUtils;
import com.hk.service.kyc.IQuestion;
import com.hk.service.sys.IAnswer;
import com.hk.service.sys.ISysUser;

@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/questionCtrl")
public class QuestionCtrl extends BaseCtrl {

    @Autowired
    private IQuestion questionService;


    private Logger logger = LoggerFactory.getLogger(QuestionCtrl.class);

    @Autowired
    public YingmiApiUtils utils;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBase<Boolean> save(@RequestBody QuestionReq req) {
        ResponseBase<Boolean> resp = new ResponseBase<>();

        Question checkExist = questionService.findBySeqNumAndGroupId(req.getSeqNum(),req.getGroupId());

        if(checkExist != null){
            logger.error("保存问题失败,存在相同分组Id和序号的记录");
            resp.setMsg("保存问题失败,存在相同分组Id和序号的记录");
            resp.setResultCode(StatusCode.EXCEPTION.key());
            return resp;
        }

        Question qu = new Question();

        qu.setQuestion(req.getQuestionArray());
        qu.setAnswer(req.getSelectionArray());
        qu.setValue(req.getSelectionValArray());
        qu.setCreater(Long.valueOf(req.getUserId()));
        qu.setGroupId(req.getGroupId());
        qu.setSeqNum(req.getSeqNum());

        try {
            Long id = questionService.add(qu);
            resp.setResult(true);
            resp.setResultCode(StatusCode.SUCCESS.key());
        } catch (Exception e) {
            logger.error("保存问题失败");
            resp.setMsg("保存问题失败，请重试");
            resp.setResultCode(StatusCode.EXCEPTION.key());
        }
        return resp;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBase<List<QuestionRes>> list(@RequestBody QuestionReq req) {
        ResponseBase<List<QuestionRes>> resp = new ResponseBase<>();
        List<QuestionRes> questionReslist = new ArrayList<>();

        HqlFilter hqlFilter = new HqlFilter();

        if (StringUtils.isNotEmpty(req.getName())) {
            hqlFilter.addLKFilter("name", req.getName());
        }


        List<Question> list = questionService.findByFilter(hqlFilter,
                req.getCurPage(), req.getPageSize());

        for (Question qu : list) {
            QuestionRes quRes = new QuestionRes();
            quRes.setId(qu.getId());
            quRes.setCts(qu.getCts());
            quRes.setCreater(qu.getCreater());
            quRes.setQuestion(qu.getQuestion());

            quRes.setGroupId(qu.getGroupId());
            quRes.setSeqNum(qu.getSeqNum());


            String[] selectionArray = null;
            String selectionStr = qu.getAnswer();
            selectionArray = selectionStr.split(",");
            quRes.setAnswer(selectionArray);

            String[] valueArray = null;
            String valueStr = qu.getValue();
            valueArray = valueStr.split(",");
            quRes.setValue(valueArray);

            questionReslist.add(quRes);
        }

        resp.setSize(questionReslist.size());
        resp.setResult(questionReslist);
        return resp;
    }
}
